Simplify _gtk_text_buffer_get_line_log_attrs()
authorSébastien Wilmet <swilmet@gnome.org>
Wed, 20 Aug 2014 17:06:05 +0000 (19:06 +0200)
committerSébastien Wilmet <swilmet@gnome.org>
Thu, 21 Aug 2014 16:43:34 +0000 (18:43 +0200)
NULL was returned in case of an empty last line. Every users needed to
special-case this. Now it will return the expected result: char_len of 0
with one PangoLogAttr.

In compute_log_attrs(), 'paragraph' will be the empty string "" with
'char_len' == 0.
pango_get_log_attrs() works fine with an empty string, it will return
one correct PangoLogAttr (because there is one text position for the
empty string).

It fixes the unit tests for gtk_text_iter_is_cursor_position().

https://bugzilla.gnome.org/show_bug.cgi?id=156164

gtk/gtktextbuffer.c
gtk/gtktextiter.c
testsuite/gtk/textiter.c

index 38620ef20fb55612bf432fad649e9a0a5d865968..65dfd8550f07f9c96773caec3111ba358a80a57f 100644 (file)
@@ -3889,6 +3889,7 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer,
   GtkTextIter end;
   gboolean retval = FALSE;
   const PangoLogAttr *attrs;
+  gint offset;
   gboolean backspace_deletes_character;
 
   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
@@ -3898,17 +3899,8 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer,
   end = *iter;
 
   attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
-
-  /* For no good reason, attrs is NULL for the empty last line in
-   * a buffer. Special case that here. (#156164)
-   */
-  if (attrs != NULL)
-    {
-      gint offset = gtk_text_iter_get_line_offset (&start);
-      backspace_deletes_character = attrs[offset].backspace_deletes_character;
-    }
-  else
-    backspace_deletes_character = FALSE;
+  offset = gtk_text_iter_get_line_offset (&start);
+  backspace_deletes_character = attrs[offset].backspace_deletes_character;
 
   gtk_text_iter_backward_cursor_position (&start);
 
@@ -4325,8 +4317,6 @@ compute_log_attrs (const GtkTextIter *iter,
   char_len = g_utf8_strlen (paragraph, -1);
   byte_len = strlen (paragraph);
 
-  g_assert (char_len > 0);
-
   if (char_lenp != NULL)
     *char_lenp = char_len;
 
@@ -4346,6 +4336,7 @@ compute_log_attrs (const GtkTextIter *iter,
 }
 
 /* The return value from this is valid until you call this a second time.
+ * Returns (char_len + 1) PangoLogAttr's, one for each text position.
  */
 const PangoLogAttr *
 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer     *buffer,
@@ -4362,15 +4353,6 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer     *buffer,
 
   priv = buffer->priv;
 
-  /* special-case for empty last line in buffer */
-  if (gtk_text_iter_is_end (anywhere_in_line) &&
-      gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
-    {
-      if (char_len != NULL)
-        *char_len = 0;
-      return NULL;
-    }
-  
   /* FIXME we also need to recompute log attrs if the language tag at
    * the start of a paragraph changes
    */
index 7b58cb470de290c14587a431639641e213e4cdba..38cb1752bc58a2f7a4a13ba86484d5f955c54162 100644 (file)
@@ -3060,7 +3060,6 @@ test_log_attrs (const GtkTextIter *iter,
   gint char_len;
   const PangoLogAttr *attrs;
   gint offset;
-  gboolean result = FALSE;
 
   g_return_val_if_fail (iter != NULL, FALSE);
 
@@ -3069,16 +3068,9 @@ test_log_attrs (const GtkTextIter *iter,
 
   offset = gtk_text_iter_get_line_offset (iter);
 
-  /* char_len may be 0 and attrs will be NULL if so, if
-   * iter is the end iter and the last line is empty.
-   *
-   * offset may be equal to char_len, since attrs contains an entry
-   * for one past the end.
-   */
-  if (attrs != NULL && offset <= char_len)
-    result = (* func) (attrs, offset, 0, char_len);
+  g_assert (offset <= char_len);
 
-  return result;
+  return (* func) (attrs, offset, 0, char_len);
 }
 
 static gboolean
@@ -3090,7 +3082,6 @@ find_line_log_attrs (const GtkTextIter *iter,
   gint char_len;
   const PangoLogAttr *attrs;
   gint offset;
-  gboolean result = FALSE;
 
   g_return_val_if_fail (iter != NULL, FALSE);
   
@@ -3098,15 +3089,12 @@ find_line_log_attrs (const GtkTextIter *iter,
                                                iter, &char_len);      
 
   offset = gtk_text_iter_get_line_offset (iter);
-  
-  /* char_len may be 0 and attrs will be NULL if so, if
-   * iter is the end iter and the last line is empty.
-   */
-  if (attrs != NULL)
-    result = (* func) (attrs, offset, char_len, found_offset,
-                       already_moved_initially);
 
-  return result;
+  return (* func) (attrs,
+                   offset,
+                   char_len,
+                   found_offset,
+                   already_moved_initially);
 }
 
 static gboolean
index 21c18699f6004bc36da46e5de661c449b855760a..d05a8972de929444cda5c16b966db9a1f63546c0 100644 (file)
@@ -538,8 +538,8 @@ test_cursor_positions (void)
   check_is_cursor_position ("a\r\n", 0, TRUE);
   check_is_cursor_position ("a\r\n", 1, TRUE);
   check_is_cursor_position ("a\r\n", 2, FALSE);
-  check_is_cursor_position ("a\r\n", 3, FALSE); /* FIXME should be TRUE */
-  check_is_cursor_position ("", 0, FALSE);      /* FIXME should be TRUE */
+  check_is_cursor_position ("a\r\n", 3, TRUE);
+  check_is_cursor_position ("", 0, TRUE);
 
   /* forward */
   check_cursor_position ("a\r\nb", TRUE, 0, 1, TRUE);